TextBoxer
Volume Number: 8
Issue Number: 3
Column Tag: Getting Started
A vehicle for experimenting with QuickDraw's text and shape-drawing
routines
By Dave Mark, MacTutor Regular Contributing Author
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
About the author
Dave Mark is an accomplished Macintosh author and an Apple Partner. He is the
author of The Macintosh Programming Primer Series which includes: Macintosh C
Programming Primer, Volumes 1 and 2; Macintosh Pascal Programming Primer,
Volume 1, and his latest book, Learn C on the Macintosh. These books are available
through the MacTutor Mail Order Store located in the back of the magazine. Dave is also
the “professor” on the Learn Programming Forum on CompuServe. To get there, type
GO MACDEV, then check out section 11.
In last months column, we covered the basics of programming using windows and
QuickDraw. The Macintosh Toolbox was introduced, and a function was presented that
properly initializes the Toolbox. This month, we’ll dig a little deeper into the
relationship between windows and QuickDraw.
TextBoxer: Still Life With Text and QuickDraw
This months application, TextBoxer, gives you a vehicle to experiment with
QuickDraw’s text and shape-drawing routines. In its initial incarnation, TextBoxer
creates the window shown in Figure 1. Notice that the content region of the window
contains two rectangle (one inside the other), as well as a text string.
Figure 1. TextBoxer in action.
Creating the TextBoxer Project
Launch THINK C, creating a new project called TextBoxer.π (The π character is
created by typing option-p). THINK C will create a project window with the title
TextBoxer.π. Select New from the File menu to create a new source code window. Type
the following source code into the window:
/* 1*/
#define kVisible false
#define kMoveToFront (WindowPtr)-1L
#define kNoGoAway false
#define kNilRefCon 0L
#define kPascalString "\pAll applaud the strongly-jawed
#define kFontSize 12
#define kBottomOffset 7
#define kLeftOffset 7
void ToolBoxInit( void );
WindowPtr WindowInit( void );
/****************** main ************/
main()
{
Rect shapeRect;
WindowPtr window;
ToolBoxInit();
window = WindowInit();
shapeRect = window->portRect;
InsetRect( &shapeRect, 5, 5 );
FrameRect( &shapeRect );
InsetRect( &shapeRect, 2, 2 );
FrameRect( &shapeRect );
TextFont( monaco );
TextSize( kFontSize );
MoveTo( shapeRect.left + kLeftOffset,
shapeRect.bottom - kBottomOffset );
DrawString( kPascalString );
while ( ! Button() ) ;
}
/****************** ToolBoxInit ***********/
void ToolBoxInit( void )
{
InitGraf( &thePort );
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs( nil );
InitCursor();
}
/****************** WindowInit *************/
WindowPtr WindowInit( void )
{
WindowPtr window;
Rect windowRect;
SetRect( & windowRect, 20, 40, 260, 74 );
window = NewWindow( nil, & windowRect, "\pBox o' Text",
kVisible, documentProc, kMoveToFront,
kNoGoAway, kNilRefCon );
if ( window == nil )
{
SysBeep( 10 ); /* Couldn't create a window!!! */
ExitToShell();
}
ShowWindow( window );
SetPort( window );
return( window );
}
Select Save from the File menu and save the source code under the name
TextBoxer.c. Next, select Add (not Add...) from the Source menu to add TextBoxer.c to
the project. Finally, select Add... from the Source menu and add the MacTraps library to
the project. You’ll find MacTraps inside your Development folder, inside the THINK C
Folder, inside the Mac Libraries folder. As mentioned last month, MacTraps contains the
interfaces to the routines that make up the Macintosh Toolbox.
Once MacTraps has been added to the project, the project window should look like
the one shown in Figure 2.
Figure 2. The TextBoxer project window, before compilation.
Running TextBoxer.π
Select Run from the Project menu, asking THINK C to compile and run your
project. If you run into any compile or link errors, check the code over carefully. Once
your project runs, you should see something similar to Figure 3. To exit TextBoxer,
just click the mouse button.
Figure 3. Running TextBoxer.
Walking Through the Source Code
TextBoxer.c consists of three routines, main(), ToolBoxInit(), and
WindowInit(). As usual, we start off by defining some useful constants. I’ll explain each
of these as they occur in context.
/* 2 */
#define kVisible false
#define kMoveToFront (WindowPtr)-1L
#define kNoGoAway false
#define kNilRefCon 0L
#define kPascalString "\pAll applaud the strongly-jawed
#define kFontSize 12
#define kBottomOffset 7
#define kLeftOffset 7
Next come the function prototypes. Be sure to prototype all your functions. This
practice will go a long way towards catching compile errors.
/* 3 */
void ToolBoxInit( void );
WindowPtr WindowInit( void );
main() starts off with a couple of local variable declarations. shapeRect is
declared to be of type Rect. Rect is a widely used Toolbox type and is defined in Inside
Macintosh. A Rect has four fields: left, top, right, and bottom. Typically, you’ll fill a
Rect’s fields so they define the position and size of a rectangle.
You can set the fields of a Rect individually, like this:
/* 4 */
Rect myRect;
myRect.left = 20;
myRect.top = 30;
myRect.right = 40;
myRect.bottom = 50;
or you can use a Toolbox routine like SetRect(). SetRect() is defined in IM(I:174):
/* 5 */
SetRect( Rect *myRect, int left, int top, int right, int bottom );
The second local variable in main() is window, used to store a pointer to the
TextBoxer window.
/* 6 */
/****************** main ************/
main()
{
Rect shapeRect;
WindowPtr window;